package com.flyqiu.form.api.invoke;


import com.alibaba.fastjson2.JSONObject;
import com.flyqiu.common.enums.FlyQiuEnumTools;
import com.flyqiu.common.mybaits.plus.FlyQiuQueryWrapper;
import com.flyqiu.form.common.FlyQiuFormDataDelMode;
import com.flyqiu.form.model.entity.FlyQiuTableConfig;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;

import static com.flyqiu.form.common.FlyQiuFormConstant.FormBaseColumns.COLUMN_DELETE_FLAG;

public class FlyQiuFormDataDeleteInvoke {
    @Getter
    @Schema(title = "数据删除模式")
    private final FlyQiuFormDataDelMode dataDelMode;
    @Schema(title = "数据删除标记", description = "数据删除标记 1")
    private final int dataDelFlag;
    @Schema(title = "数据未删除标记", description = "数据未删除标记 0 ")
    private final int dataNotDelFlag;

    private FlyQiuFormDataDeleteInvoke(FlyQiuTableConfig config) {
        dataDelMode = FlyQiuEnumTools.findEnumByValue(FlyQiuFormDataDelMode.class, config.getDataDelMode(), FlyQiuFormDataDelMode.PHYSICS_DEL);
        dataDelFlag = config.getDataDelFlag() == null ? 1 : config.getDataDelFlag();
        dataNotDelFlag = config.getDataNotDelFlag() == null ? 1 : config.getDataNotDelFlag();
    }

    public static FlyQiuFormDataDeleteInvoke get(FlyQiuTableConfig config) {
        return new FlyQiuFormDataDeleteInvoke(config);
    }


    /**
     * 修改
     *
     * @param queryWrapper 查询条件
     * @param data         数据
     * @return 修改后的数据
     */
    public JSONObject invoke(FlyQiuQueryWrapper queryWrapper, JSONObject data, boolean isDel) {
        if (dataDelMode == FlyQiuFormDataDelMode.PHYSICS_DEL) {
            return data;
        }
        queryWrapper.eq(COLUMN_DELETE_FLAG, dataNotDelFlag);
        data.put(COLUMN_DELETE_FLAG, isDel ? dataDelFlag : dataNotDelFlag);
        return data;
    }


    /**
     * 新增
     *
     * @param data 数据
     */
    public void invoke(JSONObject data) {
        if (dataDelMode == FlyQiuFormDataDelMode.PHYSICS_DEL) {
            return;
        }
        data.put(COLUMN_DELETE_FLAG, dataNotDelFlag);
    }

    /**
     * 查询
     *
     * @param queryWrapper 查询条件
     */
    public void invoke(FlyQiuQueryWrapper queryWrapper) {
        if (dataDelMode == FlyQiuFormDataDelMode.PHYSICS_DEL) {
            return;
        }
        queryWrapper.eq(COLUMN_DELETE_FLAG, dataNotDelFlag);
    }
}
